package com.etoak.service.impl;

import com.etoak.entity.*;
import com.etoak.entity.dto.CheckListDto;
import com.etoak.entity.vo.CheckListVo;
import com.etoak.mapper.ApplySituationMapper;
import com.etoak.mapper.ChecklistMapper;
import com.etoak.mapper.LabelMapper;
import com.etoak.service.IChecklistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ChecklistServiceImpl implements IChecklistService {

    @Autowired
    ChecklistMapper checklistMapper;

    @Autowired
    ApplySituationMapper applySituationMapper;

    @Autowired
    LabelMapper labelMapper;

    @Override
    public List<CheckList> getChecklistItem(String encoding) {
        return checklistMapper.getChecklistItem(encoding);
    }

    @Override
    public int insertChecklist(CheckList list) {
        return checklistMapper.insertChecklist(list);
    }

    @Override
    public List<CheckList> getList(CheckList checkList) {
        return checklistMapper.getList(checkList);
    }

    @Override
    public int deleteChecklist(String[] ids) {
        return checklistMapper.deleteChecklist(ids);
    }

    @Override
    public int updateChecklist(CheckList list) {
        return checklistMapper.updateChecklist(list);
    }

    @Override
    public CheckList getChecklistItemById(String id) {
        return checklistMapper.getChecklistItemById(id);
    }

    @Override
    public void setChildren(Territory t, List<ProductLine> productlineList, List resultList) {
        for (ProductLine line:productlineList){
            //判断pid是否相同
            if (line.getTerritoryId().equals(t.getId())){
                //返回map结果集
                Map resultMap = new HashMap();
                resultMap.put("id",line.getId());
                resultMap.put("name",line.getTerritoryName());
                resultList.add(resultMap);
            }
        }
        t.setChildren(resultList);
    }

    @Override
    public void setChildren2(ProductLine line, List<Product> productList,List resultList2) {
        for(Product pro : productList){
            if(pro.getProductLineId().equals(line.getId())){
                Map resultMap = new HashMap();
                resultMap.put("id",pro.getId());
                resultMap.put("name",pro.getProductName());
                resultList2.add(resultMap);
            }
        }
        line.setChildren(resultList2);
    }

    @Override
    public List<CheckList> selectCheckList(CheckListVo checkListVo) {
        List<CheckList> checkLists = checklistMapper.selectCheckList(checkListVo);

        //拼接知识层级
        for (CheckList list : checkLists) {
            list.setKnowledgeLevel(list.getTerritoryName()+"/"+list.getProductLineName()+"/"+list.getApplicationScenarios());
        }
         return checkLists;
    }

    @Override
    public List<CheckList> getChecklistItemByTerritory(String code) {
        return checklistMapper.getChecklistItemByTerritory(code);
    }

    @Override
    public List<CheckList> topQuote(CheckList checkList) {
        return checklistMapper.topQuote(checkList);
    }


    @Override
    public  Map<String,Object> getRetrieveInformation() {
        //CheckList checklist = new CheckList();
        Map<String,Object> map = new HashMap<>();
        //设置应用场景信息
        List<ApplySituation> applySituation = applySituationMapper.getApplySituation(new ApplySituation());
        map.put("applySituation",applySituation);
        //设置标签信息
        Label label = new Label();
        List<Label> labels = labelMapper.queryList(label);
        map.put("labels",labels);
        //设置状态信息
        List<Map<String,Object>> statuss = checklistMapper.statuss();
        map.put("statuss",statuss);
        return map;
    }

    @Override
    public List<CheckListDto> getTree2() {
        return checklistMapper.getTree2();
    }

    @Override
    public List<Map<String, Object>> query1() {
        return checklistMapper.query1();
    }

    @Override
    public List<Map<String, Object>> query2(String id) {
        return checklistMapper.query2(id);
    }

    @Override
    public List<Map<String, Object>> query3(String id) {
        return checklistMapper.query3(id);
    }

    @Override
    public List<CheckList> getUnApprovedList() {
        return checklistMapper.getUnApprovedList();
    }
}
